package com.ruoyi.web.controller.pay;

import com.jianqiao.delivery.midtrans.domain.PayEntity;
import com.jianqiao.delivery.midtrans.service.MidtransPayService;
import com.midtrans.httpclient.error.MidtransError;
import com.ruoyi.common.core.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.RedirectView;

import java.math.BigDecimal;
import java.util.Map;

/**
 * author: cyx
 * create: 2024/8/7 11:49
 **/
@Slf4j
@RestController
@RequestMapping("/midtrans")
public class MidtransPayController {

    @Autowired
    private MidtransPayService midtransPayService;

    @Value("${midtrans.redirectDevUrl}")
    private String redirectDevIndexUrl;
    @Value("${midtrans.redirectProUrl}")
    private String redirectProIndexUrl;

    @Value("${midtrans.redirectDevOrderDetailsUrl}")
    private String redirectDevOrderDetailsUrl;
    @Value("${midtrans.redirectProOrderDetailsUrl}")
    private String redirectProOrderDetailsUrl;

    @Value("${midtrans.redirectDevOrderListUrl}")
    private String redirectDevOrderListUrl;
    @Value("${midtrans.redirectProOrderListUrl}")
    private String redirectProOrderListUrl;

    /**
      *methood: 生成Midtrans订单
      *auth: cyx
      *time: 2024/11/29 17:15
    **/
    @PostMapping("/createOrder")
    public AjaxResult createOrder(@RequestBody PayEntity pay) throws MidtransError {
        Map<String, String> resultMap = midtransPayService.create(pay.getOrderId(), pay.getTotalPrice());
        return AjaxResult.success(resultMap);
    }

    /**
      *methood: 测试
      *auth: cyx
      *time: 2024/11/29 17:14
    **/
    @PostMapping("/createToken")
    public AjaxResult createToken(@RequestBody Map<String ,String> paramMap) throws MidtransError {
        String idRand = paramMap.get("idRand");
        Double a = 111.0;
        BigDecimal amount = BigDecimal.valueOf(a);
        Map<String, String> resultMap = midtransPayService.create(idRand ,amount);
        return AjaxResult.success(resultMap);
    }

    @PostMapping("/refund")
    public AjaxResult refund(@RequestBody String orderId){
        String refundResponse = midtransPayService.refund(orderId);
        return AjaxResult.success(refundResponse);
    }

    @GetMapping("/callback")
    public RedirectView callback(String order_id , String status_code , String transaction_status){
        log.info("callback has been used");
        RedirectView redirectView = new RedirectView();
        //判断订单状态
        if ("201".equals(status_code) && "pending".equals(transaction_status)){
            redirectView.setUrl(redirectProOrderDetailsUrl + order_id);
        } else if ("200".equals(status_code) && ("settlement".equals(transaction_status) || "capture".equals(transaction_status))){
            //执行订单状态修改逻辑
            midtransPayService.callBack(order_id);
            //重定向回订单页
            redirectView.setUrl(redirectProOrderListUrl);
        } else {
            redirectView.setUrl(redirectProIndexUrl);
        }

        return redirectView;
    }

    @PostMapping("/callNotification")
    public String notification(@RequestBody Map<String, Object> payload){
        System.out.println(payload);
//        System.out.println(order_id);
//        System.out.println(status_code);
//        System.out.println(transaction_status);
//        String s = midtransPayService.callBack(order_id);
        return "redirect:http://http://192.168.2.12:8080";
    }

    @PostMapping("/unfinishCallback")
    public void unfinishCallback(){
        midtransPayService.unfinishCallback();
    }

    @PostMapping("/errorCallback")
    public void errorCallback(){
        midtransPayService.errorCallback();
    }
}
